package assist.client.gwt.view.activity;

import assist.client.gwt.service.GamesProvider;
import assist.client.gwt.service.StatsProvider;
import assist.client.gwt.view.ClientFactory;
import assist.client.gwt.view.event.EditGamesEvent;
import assist.client.gwt.view.place.SelectGamesPlace;
import assist.client.gwt.view.place.SelectStatsPlace;
import assist.client.gwt.view.place.ShowStatsPlace;
import assist.client.gwt.view.place.StatsPlace;
import assist.client.gwt.view.ui.SelectGamesView;
import assist.domain.model.GamesQuery;
import com.google.web.bindery.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class SelectGamesActivity extends AbstractAssistActivity implements 
    SelectGamesView.Presenter
{
    private ClientFactory clientFactory;
    private GamesQuery gamesQuery;
    private GamesProvider gamesProvider;
    private StatsProvider statsProvider;
 //   private SelectGamesPlace selectGamesPlace;

    public SelectGamesActivity(SelectGamesPlace place, ClientFactory clientFactory)
    {        
        this.gamesQuery = place.getGamesQuery();
        this.clientFactory = clientFactory;
        this.clientFactory.getEventBus().fireEvent(new EditGamesEvent(true));
        this.gamesProvider = clientFactory.getGamesProvider();
        if (this.gamesProvider.getGamesQuery() == null)
        {
            this.gamesProvider.setGamesQuery(this.gamesQuery);
        }
        this.statsProvider = clientFactory.getStatsProvider();
  //      this.selectGamesPlace = place;
    }
    
    @Override
    public void start(AcceptsOneWidget containerWidget, EventBus eventBus)
    {        
        SelectGamesView selectGamesView = clientFactory.getSelectGamesView();
        //Set the query, which can be used to display title info
        selectGamesView.setGamesQuery(this.gamesQuery);

        selectGamesView.setPresenter(this);
        containerWidget.setWidget(selectGamesView.asWidget());       
    }
    
    @Override
    public void goToSelectStats()
    {
        goToStatsPlace(new SelectStatsPlace(this.gamesQuery, 
                         this.statsProvider.getStatsQuery()));      
    }

    @Override
    public void goToShowStats()
    {
        goToStatsPlace(new ShowStatsPlace(this.gamesQuery, 
                         this.statsProvider.getStatsQuery()));
    }
    
    private void goToStatsPlace(StatsPlace statsPlace)
    {        
        //We assume that user has pressed load Games
        System.err.println("In SelectGamesActivity");
        System.err.println("ShowStats/EditStats must be disabled until Games are loaded.");
        System.err.println("Error message here if no games loaded.");
        System.err.println("Can override mayStop if no games loaded.");
        //this.statsProvider.setGamesQuery(this.gamesQuery);               
        clientFactory.getPlaceController().goTo(statsPlace);
    }

    @Override
    public void loadGames()
    {
        this.gamesProvider.setGamesQuery(this.gamesQuery);        
        clientFactory.getPlaceController().goTo(new SelectGamesPlace(this.gamesQuery)); 
    }
}
